package weaver.interfaces.jz.action;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.jz.service.JzjgService;
import com.engine.jz.service.impl.JzjgServiceImpl;
import com.engine.jz.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.util.FileUtil;
import weaver.workflow.request.RequestManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/11/28
 * @Description 人员入职同步设备Action
 * @Author AdminZm
 */
public class HrmEntrySyncDeviceAction implements Action {

    BaseBean baseBean = new BaseBean();

    public JzjgService getJzjgService(User user) {
        return ServiceUtil.getService(JzjgServiceImpl.class, user);
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        RequestManager requestManager = requestInfo.getRequestManager();
        int billId = requestManager.getBillid();
        String tableName = requestManager.getBillTableName();
        String requestId = requestInfo.getRequestid();
        User user = requestManager.getUser();
        baseBean.writeLog("HrmEntrySyncDeviceAction start:" + requestId + "、" + billId + "、" + tableName);
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        try {
            String gh1 = null;
            String xm = null;
            String sfzhm = null;
            String kqj = null;
            String rytx = null;
            rs.executeQuery("SELECT * FROM " + tableName + " WHERE id = ?", billId);
            if (rs.next()) {
                gh1 = rs.getString("gh");
                xm = rs.getString("xm");
                sfzhm = rs.getString("sfzhm");
                kqj = rs.getString("kqj");
                rytx = rs.getString("rytx");
            }

            // 根据工号获取loginid
            if (StringUtils.isEmpty(gh1)) {
                baseBean.writeLog("HrmEntrySyncDeviceAction gh is null.");
                return Action.SUCCESS;
            }
            String gh = null;
            String zp = null;
            rs.executeQuery("SELECT loginid,resourceimageid FROM hrmresource WHERE workcode = ?", gh1);
            if (rs.next()) {
                gh = rs.getString("loginid");
                zp = rs.getString("resourceimageid");
            }
            if (StringUtils.isEmpty(gh)) {
                baseBean.writeLog("HrmEntrySyncDeviceAction loginid is null.");
                return Action.SUCCESS;
            }

            gh = CommonUtil.removeLeadingZeros(gh);
            if (StringUtils.isEmpty(gh)) {
                baseBean.writeLog("HrmEntrySyncDeviceAction gh is null.");
                return Action.SUCCESS;
            }

            if (StringUtils.isEmpty(kqj)) {
                baseBean.writeLog("HrmEntrySyncDeviceAction kqj is null.");
                return Action.SUCCESS;
            }

            String modeId = rs.getPropValue("jzjg_zm_prop", "tssbjl_mode_id");
            String cusId = rs.getPropValue("jzjg_zm_prop", "tssbjl_cus_id");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            String tbIp = rs.getPropValue("jzjg_zm_prop", "tb_ip");
            // 查询设备信息
            rs.execute("SELECT * FROM uf_kqsbdzgxgljmb WHERE id in (" + kqj + ")");
            while (rs.next()) {
                String id = rs.getString("id");
                String sblx = rs.getString("sblx");
                String sbxlh = rs.getString("sbxlh");
                String sbip = rs.getString("sbip");

                // 天波
                if (StringUtils.equals(sblx, "0")) {
                    sbip = tbIp;
                    Map<String, Object> tbParamMap = new HashMap<>();
                    tbParamMap.put("SN", sbxlh);
                    long taskId = System.currentTimeMillis();
                    tbParamMap.put("taskId", taskId);
                    Map<String, Object> tbParamMap_copy = new HashMap<>();
                    tbParamMap_copy.put("SN", sbxlh);
                    tbParamMap_copy.put("taskId", taskId);
                    List<Map<String, Object>> mapList = new ArrayList<>();
                    List<Map<String, Object>> mapList_copy = new ArrayList<>();
                    Map<String, Object> mapTemp = new HashMap<>();
                    mapTemp.put("pin", gh);
                    mapTemp.put("name", xm);
                    mapTemp.put("userType", 0);
                    Map<String, Object> mapTemp_copy = new HashMap<>();
                    mapTemp_copy.put("pin", gh);
                    mapTemp_copy.put("name", xm);
                    mapTemp_copy.put("userType", 0);

                    if (StringUtils.isNotEmpty(rytx)) {
                        mapTemp_copy.put("photo", rytx);
                        rs1.executeQuery("SELECT docid,IMAGEFILEID FROM DOCIMAGEFILE WHERE docid=?", rytx);
                        if (rs1.next()) {
                            String imageFileId = rs1.getString("IMAGEFILEID");
                            if (StringUtils.isNotEmpty(imageFileId)) {
                                String imageBase64 = CommonUtil.imageBase64(Integer.parseInt(imageFileId));
                                mapTemp.put("photo", imageBase64);
                            }
                        }
                    }
                    mapList.add(mapTemp);
                    mapList_copy.add(mapTemp_copy);
                    tbParamMap.put("Data", mapList);
                    tbParamMap_copy.put("Data", mapList_copy);

                    String uuid = UUID.randomUUID() + "-" + gh;
                    rs1.executeUpdate("INSERT INTO uf_tssbjlglb (sb,qqid,qqcsjmq,zt,tslx, formmodeid,MODEUUID,modedatacreater," +
                                    "modedatacreatertype, modedatacreatedate, modedatacreatetime) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
                            id, requestId, JSONObject.toJSONString(tbParamMap_copy), "0", "0", modeId, uuid, "1",
                            "0", sdfDate.format(new Date()), sdfTime.format(new Date()));
                    String dataId = null;
                    rs1.executeQuery("SELECT id FROM uf_tssbjlglb WHERE MODEUUID = ?", uuid);
                    if (rs1.next()) {
                        dataId = rs1.getString("id");
                    }
                    getJzjgService(user).pushHrmToTb(sbip, tbParamMap, dataId);
                }
                // 旷视
                if (StringUtils.equals(sblx, "1")) {
                    Map<String, Object> ksParamMap = new HashMap<>();
                    // 人员id
                    ksParamMap.put("id", gh);
                    // 人员姓名
                    ksParamMap.put("person_name", xm);
                    // 人员编号
                    ksParamMap.put("person_code", gh);
                    // 身份证号码，需要aes加密
                    if (StringUtils.isNotEmpty(sfzhm)) {
                        ksParamMap.put("id_number", sfzhm);
                    }
                    // 人员头像
//                    if (StringUtils.isNotEmpty(rytx)) {
//                        rs1.executeQuery("SELECT docid,IMAGEFILEID FROM DOCIMAGEFILE WHERE docid=?", rytx);
//                        if (rs1.next()) {
//                            String imageFileId = rs1.getString("IMAGEFILEID");
//                            if (StringUtils.isNotEmpty(imageFileId)) {
//                                ksParamMap.put("rytx", rytx);
//                            }
//                        }
//                    }
                    if (StringUtils.isNotEmpty(zp)) {
                        ksParamMap.put("rytx", zp);
                    }
                    String uuid = UUID.randomUUID() + "-" + gh;
                    rs1.executeUpdate("INSERT INTO uf_tssbjlglb (sb,qqid,qqcsjmq,zt,tslx, formmodeid,MODEUUID,modedatacreater," +
                            "modedatacreatertype, modedatacreatedate, modedatacreatetime) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
                            id, requestId, JSONObject.toJSONString(ksParamMap), "0", "0", modeId, uuid, "1",
                            "0", sdfDate.format(new Date()), sdfTime.format(new Date()));
                    String dataId = null;
                    rs1.executeQuery("SELECT id FROM uf_tssbjlglb WHERE MODEUUID = ?", uuid);
                    if (rs1.next()) {
                        dataId = rs1.getString("id");
                    }
                    getJzjgService(user).pushHrmToKs(sbip, ksParamMap, dataId);
                }

                // 中控
                if (StringUtils.equals(sblx, "2")) {

                }
            }

        } catch (Exception e) {
            baseBean.writeLog("HrmEntrySyncDeviceAction error:" + e);
        }
        return Action.SUCCESS;
    }
}
